//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js')
Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        number: 0,
        avatarUrl: '/image/pc_20.png',
        avdUrl: 'http://ocae7r7wx.bkt.clouddn.com/Fj2-cD6dmNPEGoxBCjucGZcm0jZY',
        isADV: 1,
        nickName: "",
        is_login:false,
        LoginRemind:true,
        unsign:false
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    bindtapRanking: function () {
        wx.navigateTo({
            url: '../RankingList/RankingList'
        })
    },
    guangaotap: function () {
        wx.clearStorage()
    },
    StartGame: function () {
        wx.navigateTo({
            url: '../details/details'
        })
        // wx.redirectTo({
        //     url: '../details/details'
        // })
    },
    ShoppingMallTap: function () {
        wx.navigateToMiniProgram({
            appId: 'wxaa9104bb31aa9b96',
            path: 'pages/index/index',
            envVersion: 'release',
            success: function (res) {
                //console.log(res);
            },
            fail: function (res) {
                //console.error(res);
                wx.showToast({
                    title: res.data.message,
                    image: '/image/errIcon.png',
                    duration: 2000
                })
            }
        })
    },
    onLoad: function (opt) {
        wx.getSystemInfo({
            success: function(res) {
                console.log(res.platform)
                app.globalData.platform = res.platform;

            }
        })
        console.log(opt)
        var that = this;
        that.setData({
            opt:opt
        })
        that.onLogin()
        console.log("我的场景进入方式",opt)
        if(opt.scene == 1044){
            //console.log('群场景shareTicket',opt.shareTicket)
            app.globalData.shareTicket = opt.shareTicket
              wx.getShareInfo({
                  shareTicket:that.globalData.shareTicket,
                  success:function (res) {
                      //console.log(res)
                      app.globalData.Sharing={}
                      app.globalData.Sharing.errMsg = res.errMsg
                      app.globalData.Sharing.encryptedData = res.encryptedData
                      app.globalData.Sharing.iv = res.iv
                  }
              })
          }
        //console.log(JSON.stringify(that.data.userInfo) != '{}')
        wx.request({
            url: 'https://caige1.sanliwenhua.com/all_advs',
            method: "get",
            success: function (res) {
                //console.log(res, "广告")
                if (res.data.code = 200) {
                    var AdvData = res.data.data;
                    that.setData({
                        avdUrl: AdvData[0].pic,
                    })
                }
            }
        })

        wx.request({
            url: 'https://caige1.sanliwenhua.com/switch',
            method: "get",
            success: function (res) {
                //console.log(res, "开关")
                if (res.data.code = 200) {
                    var isADV = res.data.data.isADV;
                    that.setData({
                        isADV: isADV //广告开关写死
                    })
                } else {

                }
            }
        })
        wx.getStorage({
            key: 'userInfo',
            success: function (res) {
                var userInfoLocal = JSON.parse(res.data)
                that.setData({
                    avatarUrl: userInfoLocal.avatarUrl,
                    nickName: userInfoLocal.nickName,
                    checkpoint: userInfoLocal.checkpoint,
                    hasUserInfo: app.globalData.hasUserInfo,
                    userInfo:userInfoLocal
                })
            }
        })
        /*wx.getStorage({
            key: 'nickName',
            success: function (res) {
                that.setData({
                    nickName: res.data,
                })
            }
        })
        if (app.globalData.hasUserInfo) {
            that.setData({
                hasUserInfo: app.globalData.hasUserInfo,
                userInfo: app.globalData.userInfo
            })
        }*/
        //不登陆分享次数
        var mytime = new Date()
        var getFullYear = mytime.getFullYear().toString()
        var getMonth = (mytime.getMonth() + 1).toString()
        if (getMonth < 10) {
            getMonth = "0" + (mytime.getMonth() + 1).toString()
        }
        var getDate = mytime.getDate().toString()
        if (getDate < 10) {
            getDate = "0" + (mytime.getDate()).toString()
        }
        var date = getFullYear + getMonth + getDate
        //console.log(date)
        /*var shareNumber = "shareNumber"+ res3.data.data.id
        var sharedata = 'usreid:'+res3.data.data.id+',shareNum:3,time:'+date*/
        wx.getStorage({
            key: 'utime',
            success: function (res) {
                //console.log(res)
                var getShare = res.data
                //console.log(getShare)
                if (date != getShare) {
                    wx.setStorage({
                        key: 'ushareNum',
                        data: '3'
                    })
                    wx.setStorage({
                        key: 'utime',
                        data: date
                    })
                }
            },
            fail: function (res) {
                wx.setStorage({
                    key: 'ushareNum',
                    data: '3'
                })
                wx.setStorage({
                    key: 'utime',
                    data: date
                })

            }
        })
        // that.onLogin()

    },
    onLogin: function () {
        var that = this;
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    wx.getUserInfo({
                        success: function (res1) {
                            app.globalData.userInfo = res1.userInfo
                            function sortNumber(a, b) {
                                return b.createTime - a.createTime
                            }
                            var resetShareData = function (reset) {
                                var myTime = new Date();
                                var today = myTime.getDate();
                                if (reset) {
                                    today = 60
                                }
                                wx.getStorage({
                                    key: 'shareObj',
                                    success: function (res) {
                                        var shareObj = JSON.parse(res.data)
                                        //console.log('shareObj', shareObj)
                                        if (today != shareObj.day) {
                                            shareObj = {
                                                day: today,
                                                shareTime: 3
                                            }
                                        }
                                        wx.setStorage({
                                            key: 'shareObj',
                                            data: JSON.stringify(shareObj)
                                        })
                                        app.globalData.shareObj = shareObj

                                    }, fail: function (res) {
                                        //console.log(res)
                                        var shareObj = {
                                            day: today,
                                            shareTime: 3
                                        }
                                        wx.setStorage({
                                            key: 'shareObj',
                                            data: JSON.stringify(shareObj)
                                        })
                                        app.globalData.shareObj = shareObj
                                    }
                                })

                            }
                            wx.getStorage({
                                key: 'firstLogin',
                                success: function (res) {

                                },
                                fail: function () {
                                    resetShareData(true)// 分享次数回到3次
                                    wx.setStorage({
                                        key: 'firstLogin',
                                        data: true
                                    })
                                }
                            })
                            wx.getSavedFileList({
                                success: function (res) {
                                    res.fileList.sort(sortNumber)
                                    //console.log(res.fileList)
                                    res.fileList.map(function (item, index) {
                                        //console.log(index)
                                        if (index > 4) {
                                            wx.removeSavedFile({ filePath: item.filePath })
                                        }
                                    })
                                }
                            })
                            var userInfoOnlineStr = JSON.stringify(res1.userInfo)
                            /*wx.setStorageSync({
                                key: 'userInfo',
                                data: userInfoOnlineStr
                            })*/
                            try {
                                wx.setStorageSync('userInfo', userInfoOnlineStr)
                            } catch (e) {
                            }
                            wx.login({
                                success: function (res) {
                                    app.globalData.code = res.code;
                                    //console.log(app.globalData.code)
                                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                                    var dataSend = {
                                        code: res.code,
                                        encryptedData: res1.encryptedData,
                                        iv: res1.iv,
                                        icon: res1.userInfo.avatarUrl,
                                        nickname: res1.userInfo.nickName
                                    }
                                    //console.log('dataSend', dataSend)
                                    wx.request({
                                        url: 'https://caige1.sanliwenhua.com/wx/onLogin',
                                        method: "get",
                                        data: dataSend,
                                        success: function (res3) {
                                            //console.log('登录主机后获得的数据',res3)
                                            if (res3.data.code == 200) {
                                                // app.globalData.Sharing = {}
                                                //console.log('获取到这里的token',res3.data.data.x_token)
                                                app.globalData.userInfo.token = res3.data.data.x_token;
                                                app.globalData.userInfo.checkpoint = res3.data.data.checkpoint;
                                                app.globalData.userInfo.id = res3.data.data.id;
                                                app.globalData.session_key = res3.data.data.session_key;
                                                app.globalData.hasUserInfo = true;
                                                //console.log(app.globalData);
                                                that.setData({
                                                    userInfo: app.globalData.userInfo,
                                                    hasUserInfo: app.globalData.hasUserInfo,
                                                    avatarUrl:res1.userInfo.avatarUrl,
                                                    nickName: res1.userInfo.nickName,
                                                    checkpoint: res3.data.data.checkpoint
                                                })
                                                wx.getStorage({
                                                    key: 'userInfo',
                                                    success: function (res) {
                                                        //console.log(res.data)
                                                        var userInfoLocal = {}
                                                        userInfoLocal = JSON.parse(res.data)
                                                        //console.log(userInfoLocal)
                                                        userInfoLocal.token = res3.data.data.x_token;
                                                        userInfoLocal.checkpoint = res3.data.data.checkpoint;
                                                        wx.setStorage({
                                                            key: 'userInfo',
                                                            data: JSON.stringify(userInfoLocal)
                                                        })
                                                    },
                                                    fail: function () {

                                                    }
                                                })
                                                //分享次数缓存
                                                var mytime = new Date()
                                                var getFullYear = mytime.getFullYear().toString()
                                                var getMonth = (mytime.getMonth() + 1).toString()
                                                if (getMonth < 10) {
                                                    getMonth = "0" + (mytime.getMonth() + 1).toString()
                                                }
                                                var getDate = mytime.getDate().toString()
                                                if (getDate < 10) {
                                                    getDate = "0" + (mytime.getDate()).toString()
                                                }
                                                var date = getFullYear + getMonth + getDate
                                                //console.log(date)
                                                wx.getStorage({
                                                    key: 'time',
                                                    success: function (res) {
                                                        //console.log(res)
                                                        var getShare = res.data
                                                        //console.log(getShare, date)
                                                        if (date != getShare) {
                                                            wx.setStorage({
                                                                key: 'shareNum',
                                                                data: '3'
                                                            })
                                                            wx.setStorage({
                                                                key: 'time',
                                                                data: date + ''
                                                            })
                                                        } else {
                                                            wx.getStorage({
                                                                key: 'shareNum',
                                                                success: function (res) {
                                                                    //console.log(res)
                                                                }
                                                            })
                                                            wx.getStorage({
                                                                key: 'time',
                                                                success: function (res) {
                                                                    //console.log(res)
                                                                }
                                                            })
                                                        }
                                                    },
                                                    fail: function (res) {
                                                        wx.setStorage({
                                                            key: 'shareNum',
                                                            data: '3'
                                                        })
                                                        wx.setStorage({
                                                            key: 'time',
                                                            data: date + ''
                                                        })

                                                    }
                                                })
                                                // 闯关/分享加密提交
                                                if (app.globalData.userInfo) {
                                                    var NewTime = new Date().getTime().toString();
                                                    //console.log(NewTime)
                                                    // console.log(app.globalData.userInfo.id + NewTime+app.globalData.userInfo.checkpoint+ app.globalData.userInfo.id)
                                                    var key = util.sha1(app.globalData.userInfo.id + NewTime + app.globalData.userInfo.checkpoint + app.globalData.userInfo.id);
                                                    //console.log(key)
                                                    key = key.toUpperCase()
                                                    var data = {}
                                                    if(app.globalData.shareTicket){
                                                        wx.getShareInfo({
                                                            shareTicket:app.globalData.shareTicket,
                                                            success:function (res) {
                                                                console.log(res)
                                                                app.globalData.Sharing={}
                                                                app.globalData.Sharing.errMsg = res.errMsg
                                                                app.globalData.Sharing.encryptedData = res.encryptedData
                                                                app.globalData.Sharing.iv = res.iv
                                                                console.log(app.globalData.Sharing)
                                                                if (app.globalData.Sharing) {
                                                                    data = {
                                                                        key: key,
                                                                        time: NewTime,
                                                                        checkpoint: app.globalData.userInfo.checkpoint,
                                                                        x_token: app.globalData.userInfo.token,
                                                                        errMsg: app.globalData.Sharing.errMsg,
                                                                        encryptedData: app.globalData.Sharing.encryptedData,
                                                                        iv: app.globalData.Sharing.iv,
                                                                        session_key: app.globalData.session_key
                                                                    }
                                                                } else {
                                                                    data = {
                                                                        key: key,
                                                                        time: NewTime,
                                                                        checkpoint: app.globalData.userInfo.checkpoint,
                                                                        x_token: app.globalData.userInfo.token,
                                                                    }
                                                                }
                                                                wx.request({
                                                                    url: 'https://caige1.sanliwenhua.com/update_checkpoint',
                                                                    method: "post",
                                                                    data: data,
                                                                    success: function (res1) {
                                                                        //console.log('keykeykeykeykey', res1)
                                                                        app.globalData.group_id = res1.data.group_id;
                                                                        wx.getStorage({
                                                                            key: 'userInfo',
                                                                            success: function (res2) {
                                                                                //console.log(res.data)
                                                                                var userInfoLocal = {}
                                                                                userInfoLocal = JSON.parse(res2.data)
                                                                                //console.log(userInfoLocal)
                                                                                userInfoLocal.group_id = res1.data.group_id;
                                                                                wx.setStorage({
                                                                                    key: 'userInfo',
                                                                                    data: JSON.stringify(userInfoLocal)
                                                                                })
                                                                            },
                                                                            fail: function () {

                                                                            }
                                                                        })
                                                                    },
                                                                    fail: function (res2) {

                                                                    }
                                                                })
                                                            }
                                                        })

                                                    }

                                                }

                                            } else {
                                                //console.log(res3.data);
                                            }
                                        },
                                        fail: function (res3) {
                                            //console.error(res3, '登录失败');
                                        }
                                    })
                                    if(that.data.unsign){
                                        wx.navigateTo({
                                            url: '../details/details'
                                        })
                                    }
                                }

                            })

                        }
                    })
                }else{
                    /*if(that.data.is_login){
                        that.setData({
                            LoginRemind:false,
                        })
                    }else{
                        that.setData({
                            is_login:true,
                        })
                    }*/
                }
            }
        })
       /* wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success: function (res1) {
                            console.log(res1)
                            // 可以将 res 发送给后台解码出 unionId
                            app.globalData.userInfo = res1.userInfo
                            // that.setData({
                            //     avatarUrl:res.userInfo.avatarUrl
                            // })
                            console.log(res1.userInfo)
                            wx.downloadFile({
                                url: res1.userInfo.avatarUrl,
                                success: function (res) {
                                    // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
                                    var tempFilePath = res.tempFilePath
                                    wx.saveFile({
                                        tempFilePath: tempFilePath,
                                        success: function (res) {
                                            var savedFilePath = res.savedFilePath
                                            console.log('9999999savedFilePath', savedFilePath)
                                            wx.setStorage({
                                                key: 'avatarUrl',
                                                data: savedFilePath
                                            })
                                            var myTime = new Date();
                                            var today = myTime.getDate();
                                            console
                                            if (today != app.globalData.shareObj.day || that.data.avatarUrl == '/image/pc_20.png') {
                                                that.setData({
                                                    avatarUrl: savedFilePath
                                                })
                                            }
                                            wx.getStorage({
                                                key: 'userInfo',
                                                success: function (res) {
                                                    var userInfo = JSON.parse(res.data)
                                                    userInfo.avatarUrl = savedFilePath;
                                                    var userInfoStr = JSON.stringify(userInfo)
                                                    wx.setStorage({
                                                        key: 'userInfo',
                                                        data: userInfoStr
                                                    })
                                                }
                                            })
                                            function sortNumber(a, b) {
                                                return b.createTime - a.createTime
                                            }
                                            var resetShareData = function (reset) {
                                                var myTime = new Date();
                                                var today = myTime.getDate();
                                                if (reset) {
                                                    today = 60
                                                }
                                                wx.getStorage({
                                                    key: 'shareObj',
                                                    success: function (res) {
                                                        var shareObj = JSON.parse(res.data)
                                                        console.log('shareObj', shareObj)
                                                        if (today != shareObj.day) {
                                                            shareObj = {
                                                                day: today,
                                                                shareTime: 3
                                                            }
                                                        }
                                                        wx.setStorage({
                                                            key: 'shareObj',
                                                            data: JSON.stringify(shareObj)
                                                        })
                                                        app.globalData.shareObj = shareObj

                                                    }, fail: function (res) {
                                                        console.log(res)
                                                        var shareObj = {
                                                            day: today,
                                                            shareTime: 3
                                                        }
                                                        wx.setStorage({
                                                            key: 'shareObj',
                                                            data: JSON.stringify(shareObj)
                                                        })
                                                        app.globalData.shareObj = shareObj
                                                    }
                                                })

                                            }
                                            wx.getStorage({
                                                key: 'firstLogin',
                                                success: function (res) {

                                                },
                                                fail: function () {
                                                    resetShareData(true)
                                                    wx.setStorage({
                                                        key: 'firstLogin',
                                                        data: true
                                                    })
                                                }
                                            })
                                            wx.getSavedFileList({
                                                success: function (res) {
                                                    res.fileList.sort(sortNumber)
                                                    console.log(res.fileList)
                                                    res.fileList.map(function (item, index) {
                                                        console.log(index)
                                                        if (index > 4) {
                                                            wx.removeSavedFile({ filePath: item.filePath })
                                                        }
                                                    })
                                                }
                                            })
                                        }
                                    })
                                }
                            })
                            var userInfoOnlineStr = JSON.stringify(res1.userInfo)
                            wx.setStorage({
                                key: 'userInfo',
                                data: userInfoOnlineStr
                            })
                            // wx.setStorage({
                            //     key: 'avatarUrl',
                            //     data: res.userInfo.avatarUrl
                            // })
                            // wx.setStorage({
                            //     key: 'avatarUrl',
                            //     data: res1.userInfo.avatarUrl
                            // })
                            wx.setStorage({
                                key: 'nickName',
                                data: res1.userInfo.nickName
                            })
                            wx.login({
                                success: function (res) {
                                    app.globalData.code = res.code;
                                    console.log(app.globalData.code)
                                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                                    var dataSend = {
                                        code: app.globalData.code,
                                        encryptedData: res1.encryptedData,
                                        iv: res1.iv,
                                        icon: res1.userInfo.avatarUrl,
                                        nickname: res1.userInfo.nickName
                                    }
                                    console.log('dataSend', dataSend)
                                    wx.request({
                                        url: 'https://caige1.sanliwenhua.com/wx/onLogin',
                                        method: "get",
                                        data: {
                                            code: app.globalData.code,
                                            encryptedData: res1.encryptedData,
                                            iv: res1.iv,
                                            icon: res1.userInfo.avatarUrl,
                                            nickname: res1.userInfo.nickName
                                        },
                                        success: function (res3) {
                                            console.log('登录主机后获得的数据',res3)
                                            if (res3.data.code == 200) {
                                                // app.globalData.Sharing = {}
                                                console.log('获取到这里的token',res3.data.data.x_token)
                                                app.globalData.userInfo.token = res3.data.data.x_token;
                                                app.globalData.userInfo.checkpoint = res3.data.data.checkpoint;
                                                app.globalData.userInfo.id = res3.data.data.id;
                                                app.globalData.session_key = res3.data.data.session_key;
                                                wx.setStorage({
                                                    key: 'checkpoint',
                                                    data: res3.data.data.checkpoint
                                                })
                                                that.setData({
                                                    checkpoint: res3.data.data.checkpoint
                                                })
                                                console.log(app.globalData);
                                                that.setData({
                                                    userInfo: app.globalData.userInfo,
                                                    hasUserInfo: true,
                                                    // avatarUrl:res.userInfo.avatarUrl,
                                                    nickName: res1.userInfo.nickName
                                                })
                                                // try {
                                                //     var userInfoStr = wx.getStorageSync('userInfo')
                                                //     var userInfoJson = JSON.parse(userInfoStr)
                                                //     if (userInfoStr) {
                                                //         that.setData({
                                                //             avatarUrl : userInfoJson.avatarUrl
                                                //         })
                                                //         // Do something with return value
                                                //     }
                                                //   } catch (e) {
                                                //     // Do something when catch error
                                                //   }

                                                wx.getStorage({
                                                    key: 'userInfo',
                                                    success: function (res) {
                                                        console.log(res.data)
                                                        var userInfoLocal = {}
                                                        userInfoLocal = JSON.parse(res.data)
                                                        console.log(userInfoLocal)
                                                        userInfoLocal.token = res3.data.data.x_token;
                                                        userInfoLocal.checkpoint = res3.data.data.checkpoint;
                                                        wx.setStorage({
                                                            key: 'userInfo',
                                                            data: JSON.stringify(userInfoLocal)
                                                        })
                                                    },
                                                    fail: function () {

                                                    }
                                                })
                                                //分享次数缓存
                                                var mytime = new Date()
                                                var getFullYear = mytime.getFullYear().toString()
                                                var getMonth = (mytime.getMonth() + 1).toString()
                                                if (getMonth < 10) {
                                                    getMonth = "0" + (mytime.getMonth() + 1).toString()
                                                }
                                                var getDate = mytime.getDate().toString()
                                                if (getDate < 10) {
                                                    getDate = "0" + (mytime.getDate()).toString()
                                                }
                                                var date = getFullYear + getMonth + getDate
                                                console.log(date)
                                                wx.getStorage({
                                                    key: 'time',
                                                    success: function (res) {
                                                        console.log(res)
                                                        var getShare = res.data
                                                        console.log(getShare, date)
                                                        if (date != getShare) {
                                                            wx.setStorage({
                                                                key: 'shareNum',
                                                                data: '3'
                                                            })
                                                            wx.setStorage({
                                                                key: 'time',
                                                                data: date + ''
                                                            })
                                                        } else {
                                                            wx.getStorage({
                                                                key: 'shareNum',
                                                                success: function (res) {
                                                                    console.log(res)
                                                                }
                                                            })
                                                            wx.getStorage({
                                                                key: 'time',
                                                                success: function (res) {
                                                                    console.log(res)
                                                                }
                                                            })
                                                        }
                                                    },
                                                    fail: function (res) {
                                                        wx.setStorage({
                                                            key: 'shareNum',
                                                            data: '3'
                                                        })
                                                        wx.setStorage({
                                                            key: 'time',
                                                            data: date + ''
                                                        })

                                                    }
                                                })
                                                // 闯关/分享加密提交
                                                if (app.globalData.userInfo) {
                                                    var NewTime = new Date().getTime().toString();
                                                    console.log(NewTime)
                                                    // console.log(app.globalData.userInfo.id + NewTime+app.globalData.userInfo.checkpoint+ app.globalData.userInfo.id)
                                                    var key = util.sha1(app.globalData.userInfo.id + NewTime + app.globalData.userInfo.checkpoint + app.globalData.userInfo.id);
                                                    console.log(key)
                                                    key = key.toUpperCase()
                                                    var data = {}
                                                    console.log(app.globalData.Sharing)
                                                    if (app.globalData.Sharing) {
                                                        data = {
                                                            key: key,
                                                            time: NewTime,
                                                            checkpoint: app.globalData.userInfo.checkpoint,
                                                            x_token: app.globalData.userInfo.token,
                                                            errMsg: app.globalData.Sharing.errMsg,
                                                            encryptedData: app.globalData.Sharing.encryptedData,
                                                            iv: app.globalData.Sharing.iv,
                                                            session_key: app.globalData.session_key
                                                        }
                                                    } else {
                                                        data = {
                                                            key: key,
                                                            time: NewTime,
                                                            checkpoint: app.globalData.userInfo.checkpoint,
                                                            x_token: app.globalData.userInfo.token,
                                                        }
                                                    }

                                                    wx.request({
                                                        url: 'https://caige1.sanliwenhua.com/update_checkpoint',
                                                        method: "post",
                                                        data: data,
                                                        success: function (res) {
                                                            console.log('keykeykeykeykey', res)
                                                            app.globalData.group_id = res.data.group_id
                                                        },
                                                        fail: function (res) {

                                                        }
                                                    })
                                                }
                                            } else {
                                                console.log(res3.data);
                                            }
                                        },
                                        fail: function (res3) {
                                            console.error(res3, '登录失败');
                                        }
                                    })
                                }
                            })
                            // if (that.userInfoReadyCallback) {
                            //   that.userInfoReadyCallback(res)
                            // }
                        }
                    })
                }
            }
        })*/
    },
    PromptSecond:function () {
        this.setData({
            LoginRemind:true
        })
    },
    getUserInfo: function (e) {
        //console.log(e)
        /*app.globalData.userInfo = e.detail.userInfo;*/
        this.setData({
            unsign:true
        })
        this.onLogin();
    },
    onShow: function () {
        //console.log(app.globalData)

        var that = this;

        wx.stopVoice()
        var pages = getCurrentPages()
        //console.log(pages)
        // that.sound = http://p9ofir1eb.bkt.clouddn.com/empty.mp3
        const backgroundAudioManager = wx.getBackgroundAudioManager()
        //console.log(backgroundAudioManager)
        backgroundAudioManager.stop()
        if (app.audioCtx) {
            app.audioCtx.pause()

            app.audioCtx.stop()
        }
        app.globalData.isDetailPage = false;
        // wx.getSavedFileList({
        //     success: function (res) {
        //         if(res.fileList[0]){
        //             that.setData({
        //                 avatarUrl:res.fileList[0].filePath
        //             })
        //         }
        //     }
        // })
        // for (let i = 0; i <10; i++) {
        //     setTimeout(function () {
        //         wx.getSavedFileList({
        //             success: function (res) {
        //                 that.setData({
        //                     avatarUrl:res.fileList[0].filePath
        //                 })
        //             }
        //         })
        //     },200*i)
        // }
        //console.log(11547, that.data.avatarUrl)
        wx.getStorage({
            key: 'userInfo',
            success: function (res) {
                console.log(res.data)
                var userInfoLocal = {}
                userInfoLocal = JSON.parse(res.data)
                console.log(userInfoLocal)
                if (app.globalData.hasUserInfo) {
                    that.setData({
                        hasUserInfo: app.globalData.hasUserInfo,
                        userInfo: userInfoLocal,
                        nickName:userInfoLocal.nickName,
                        avatarUrl:userInfoLocal.avatarUrl,
                        checkpoint:userInfoLocal.checkpoint
                    })
                }

            },
            fail: function () {

            }
        })
        /*wx.getStorage({
            key: 'checkpoint',
            success: function (res) {
                that.setData({
                    checkpoint: res.data
                })
            },
            fail: function (res) {
                that.setData({
                    checkpoint: 0
                })
            }
        })*/
        wx.showShareMenu({
            withShareTicket: true
        })

        //console.log(5555, this.fromShareBack)
        //如果是从分享成功进来的
        if (this.fromShareBack && app.globalData.platform == "ios"){
            wx.getShareInfo({
                shareTicket: app.globalData.shareTicket,
                success: function (res) {
                    console.log(res)
                    app.globalData.Sharing = {}
                    app.globalData.Sharing.errMsg = res.errMsg
                    app.globalData.Sharing.encryptedData = res.encryptedData
                    app.globalData.Sharing.iv = res.iv
                }
            })
            console.log(app.globalData.Sharing)
            this.fromShareBack = false;
        }
    },
    onShareAppMessage: function (res) {
        var that = this;
        // _this.data.pic = 'http://p4svid6ld.bkt.clouddn.com/image/zhuanbi.jpg'; //测试修改
        if (res.from === 'button') {
            //console.log(res.target)
        }
        return {
            title: '[@我]想要与您进行猜歌挑战，点击进入！',
            path: 'pages/index/index',
            imageUrl: '/image/pc_03.jpg',

            success: function (res) {
                wx.showToast({
                    title: '分享成功!',
                    icon: 'success',
                    duration: 2000
                })
                console.log('分享到群的标记', res.shareTickets[0])
                if(res.shareTickets){
                    app.globalData.shareTicket = res.shareTickets[0]
                    that.fromShareBack = true;

                    if(app.globalData.platform == "android"){
                        wx.getShareInfo({
                            shareTicket: app.globalData.shareTicket,
                            success: function (res) {
                                console.log(res)
                                app.globalData.Sharing = {}
                                app.globalData.Sharing.errMsg = res.errMsg
                                app.globalData.Sharing.encryptedData = res.encryptedData
                                app.globalData.Sharing.iv = res.iv
                            }
                        })
                        console.log(app.globalData.Sharing)
                        that.fromShareBack = false;
                    }
                }
            },
            fail: function (res) {
                wx.showToast({
                    title: '分享失败！',
                    image: '/image/errIcon.png',
                    duration: 2000
                })
            }
        }
    }
})
